package com.okbihuo.perm.core.auth.endpoint;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.okbihuo.perm.core.tool.support.Kv;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@ApiSort(1)
@RestController
@RequiredArgsConstructor
@Api(value = "用户授权认证", tags = "授权接口")
public class LoginEndPoint {
    
    @RequestMapping("test")
    public String test() {
        return "aaaa";
    }

//    @ApiLog("登录用户验证")
    @RequestMapping("/oauth/token")
    @ApiOperation(value = "获取认证令牌", notes = "传入租户ID:tenantId,账号:account,密码:password")
    public Kv token(@ApiParam(value = "租户ID", required = true) @RequestParam String tenantId,
                    @ApiParam(value = "账号", required = true) @RequestParam(required = false) String username,
                    @ApiParam(value = "密码", required = true) @RequestParam(required = false) String password) {
//
//        Kv authInfo = Kv.create();
//
//        String grantType = WebUtil.getRequest().getParameter("grant_type");
//        String refreshToken = WebUtil.getRequest().getParameter("refresh_token");
//
//        String userType = Func.toStr(WebUtil.getRequest().getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE);
//
//        TokenParameter tokenParameter = new TokenParameter();
//        tokenParameter.getArgs().set("tenantId", tenantId).set("username", username).set("password", password).set("grantType", grantType).set("refreshToken", refreshToken).set("userType", userType);
//
//        ITokenGranter granter = TokenGranterBuilder.getGranter(grantType);
//        UserInfo userInfo = granter.grant(tokenParameter);
//
//        if (userInfo == null || userInfo.getUser() == null) {
//            return authInfo.set("error_code", HttpServletResponse.SC_BAD_REQUEST).set("error_description", "用户名或密码不正确");
//        }
//
//        if (Func.isEmpty(userInfo.getRoles())) {
//            return authInfo.set("error_code", HttpServletResponse.SC_BAD_REQUEST).set("error_description", "未获得用户的角色信息");
//        }
//
//        return TokenUtil.createAuthInfo(userInfo);
        return null;
    }


    @GetMapping("/oauth/logout")
    @ApiOperation(value = "退出登录")
    public Kv logout() {
//        User user = AuthUtil.getUser();
//        if (user != null && jwtProperties.getState()) {
//            String token = JwtUtil.getToken(WebUtil.getRequest().getHeader(TokenConstant.HEADER));
//            JwtUtil.removeAccessToken(user.getTenantId(), String.valueOf(user.getUserId()), token);
//        }
//        return Kv.create().set("success", "true").set("msg", "success");
        return null;
    }

    @GetMapping("/oauth/captcha")
    @ApiOperation(value = "获取验证码")
    public Kv captcha() {
//        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
//        String verCode = specCaptcha.text().toLowerCase();
//        String key = UUID.randomUUID().toString();
//        // 存入redis并设置过期时间为30分钟
//        bladeRedis.setEx(CacheNames.CAPTCHA_KEY + key, verCode, Duration.ofMinutes(30));
//        // 将key和base64返回给前端
//        return Kv.create().set("key", key).set("image", specCaptcha.toBase64());
        return null;
    }

    @GetMapping("/oauth/clear-cache")
    @ApiOperation(value = "清除缓存")
    public Kv clearCache() {
//        CacheUtil.clear(CacheConstant.BIZ_CACHE);
//        CacheUtil.clear(CacheConstant.USER_CACHE);
//        CacheUtil.clear(CacheConstant.DICT_CACHE);
//        CacheUtil.clear(CacheConstant.FLOW_CACHE);
//        CacheUtil.clear(CacheConstant.SYS_CACHE);
//        CacheUtil.clear(CacheConstant.PARAM_CACHE);
//        CacheUtil.clear(CacheConstant.RESOURCE_CACHE);
//        CacheUtil.clear(CacheConstant.MENU_CACHE);
//        CacheUtil.clear(CacheConstant.MENU_CACHE, Boolean.FALSE);
//        return Kv.create().set("success", "true").set("msg", "success");
        return null;
    }
}
